#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/7/31 14:20
# @USER    : Shengji He
# @File    : MagicIndex.py
# @Software: PyCharm
# @Version  : Python-
# @TASK:
from typing import List


class Solution:
    def findMagicIndex(self, nums: List[int]) -> int:
        """
        A magic index in an array A[0...n-1] is defined to be an index such that A[i] = i. Given a sorted array
        of integers, write a method to find a magic index, if one exists, in array A. If not, return -1.
        If there are more than one magic index, return the smallest one.

        Example1:
            Input: nums = [0, 2, 3, 4, 5]

            Output: 0
        Example2:
            Input: nums = [1, 1, 1]
            Output: 1
        Note:
            - 1 <= nums.length <= 1000000
            - This problem is the follow-up of the original problem in the book, i.e. the values are not distinct.

        :param nums:
        :return:
        """
        for i in range(len(nums)):
            if nums[i] == i:
                return i
        return -1


if __name__ == '__main__':
    S = Solution()
    nums = [0, 2, 3, 4, 5]
    print(S.findMagicIndex(nums))
    print('done')
